import math
import random
from typing import List

import time
import KBEngine
import json
import KBEDebug as log

# 每20分钟恢复一点体力
ENERGY_RECOVER_ONE_PER_SECONDS = 20 * 60


def dailyRefresh(self):
    self.seasonCount = 3
    self.freePlayCount = 3
    self.shareCount = 0
    self.seasonShareCount = 0
    self.todayPublicCnt = 0
    self.todaySeasonCnt = 0


def startGame(self, gameType):
    log.DEBUG_MSG(f'startGame: {self.owner.playerId} {gameType}')
    rand = random.randint(1, 100)
    usingType = 0
    if gameType == 1:
        difficulty = 1
        if self.freePlayCount > 0:
            self.freePlayCount -= 1
        elif self.shareCount > 0:
            self.shareCount -= 1
            usingType = 1
        else:
            if self.coin < 100:
                self.client.onGameStart(1, gameType, difficulty, usingType)
                return
            else:
                self.coin -= 100
                usingType = 2
        self.todayPublicCnt += 1
    else:
        difficulty = 2
        if self.seasonCount > 0:
            self.seasonCount -= 1
        else:
            if self.coin < 100:
                self.client.onGameStart(1, gameType, difficulty, usingType)
                return
            else:
                usingType = 2
                self.coin -= 100
        self.todaySeasonCnt += 1

    KBEngine.baseAppData['Lobby'].addChallengeCount(self.owner.playerId)
    self.client.onGameStart(0, gameType, difficulty, usingType)


def gameFinish(self, is_win, gameMode):
    log.INFO_MSG(f'gameFinish[{self.owner.databaseID}] [{self.owner.playerId}] is_win: {is_win}')
    if is_win == 0:
        self.catchCount += 1
        self.point += 100
        self.owner.rank.updateCatchCount()
        KBEngine.baseAppData['Lobby'].addCompleteCount(self.owner.playerId, self.provinceId)
        if gameMode == 1:  # normal难度
            self.owner.task.completeTask(3)
        else:  # hard难度
            self.owner.task.completeTask(4)
        # 赛季通关给鸡
        self.owner.farm.rewardChicken(gameMode)
    self.client.onGameEnd(0)
